package com.hnevc.androidui9;

import android.graphics.ImageFormat;
import android.graphics.drawable.Icon;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView tvCity;
    TextView tvTemp;
    TextView tv_weather;
    TextView tvwind;
    TextView tvpm;
    ImageView iv_icon;

    Button btnsh, btnbj, btngz;
    List<WeatherInfo> weatherInfos ;
    int index =0;
    int  iconNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initviwe();
        initEvent();
        initData();

    }

    private void initData() {
        InputStream is =getResources().openRawResource(R.raw.weather1);
        try {
            weatherInfos = WeatherService.getInfosFromXML(is);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        updateWeather();


    }

    private void updateWeather() {
        WeatherInfo weatherInfo =weatherInfos.get(index);
        tvCity.setText(weatherInfo.getName());
        tvTemp.setText(weatherInfo.getTemp());
        tv_weather.setText(weatherInfo.getWeather());
        tvwind.setText(weatherInfo.getWind());
        tvpm.setText(weatherInfo.getPm());



        
    }

    private void initEvent() {
        btnsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index=0;
                updateWeather();
                iv_icon.setImageResource(R.drawable.cloud_sun);



            }
        });
        btnbj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index =1;
                updateWeather();
                iv_icon.setImageResource(R.drawable.sun);
            }
        });
        btngz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index = 2;
                iv_icon.setImageResource(R.drawable.clouds);


                updateWeather();

            }
        });
    }

    private void initviwe() {
        tvCity = findViewById(R.id.tv_city);
        tvTemp = findViewById(R.id.tv_temp);
        btnbj = findViewById(R.id.btn_bj);
        btngz = findViewById(R.id.btn_gz);
        btnsh = findViewById(R.id.btn_sh);
        tv_weather=findViewById(R.id.tv_weather);
        tvwind=findViewById(R.id.tv_wind);
        tvpm=findViewById(R.id.tv_pm);
        iv_icon=findViewById(R.id.iv_icon);

    }
}
